package com.assets.asset.annotation.aop;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.assets.asset.mapper.TAssetMapper;
import com.assets.system.mapper.SysDeptMapper;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component  //注入容器
@Aspect  //切面
public class TurnDeptIdAOP {


    @Autowired
    private TAssetMapper tAssetMapper;

    @Autowired
    private SysDeptMapper deptMapper;

    @Around("@annotation(com.assets.asset.annotation.TurnDeptID)")
//    public void around(JoinPoint point) throws Throwable {
    public Object around(ProceedingJoinPoint point) throws Throwable {


        //获取当前
//        Object[] args = joinPoint.getArgs();
        Object[] args = point.getArgs();
        Object result = point.proceed(args);

        //拿到部门的id

        String toJSONString = JSON.toJSONString(args);
        System.out.println("toJSONString = " + toJSONString);

        //将部门的id转为所对应的名字

        String s = toJSONString.replace("]", "");
        String replace = s.replace("[", "");
        System.out.println(replace);

        JSONObject jsonObject = JSON.parseObject(replace);
        String assetCode = jsonObject.getString("assetCode");


        //将名字再存数据库
        tAssetMapper.updateDept(assetCode);
//        tAssetMapper.updateCom(assetCode);
//        tAssetMapper.updateUseCom(assetCode);


        return result;


    }


}


//        Object result = joinPoint.proceed();
//所属公司名
//        update t_asset set asset_company =
//                (select ad.dpname from
//        (select d.dept_name dpname from t_asset a,sys_dept d where d.dept_id = a.asset_company)ad)
//        where asset_code = 'M-001'


//使用公司名
//        update t_asset set asset_use_company =
//                (select ad.dpname from
//        (select d.dept_name dpname from t_asset a,sys_dept d where d.dept_id = a.asset_use_company)ad)
//        where asset_code = 'M-001'

//部门名
//        update t_asset set asset_department =
//                (select ad.dpname from
//        (select d.dept_name dpname from t_asset a,sys_dept d where d.dept_id = a.asset_department)ad)
//        where asset_code = 'M-001'
